package com.tianle.flutter_app_install

import android.app.Activity
import android.content.Context
import android.os.Build
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** FlutterAppInstallPlugin */
class FlutterAppInstallPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private lateinit var channel: MethodChannel
    private lateinit var applicationContext: Context
    private var mActivity: Activity? = null

    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.tianle.flutter_app_install")
        channel.setMethodCallHandler(this)
        applicationContext = flutterPluginBinding.applicationContext
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        when (call.method) {
            "getPlatformVersion" -> {
                result.success("Android ${Build.VERSION.RELEASE}")
            }
            "getVersionName" -> {
                result.success(
                    applicationContext.packageManager.getPackageInfo(
                        applicationContext.packageName,
                        0
                    ).versionName
                )
            }
            "getVersionCode" -> {
                result.success(
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) applicationContext.packageManager.getPackageInfo(
                        applicationContext.packageName,
                        0
                    ).longVersionCode else applicationContext.packageManager.getPackageInfo(
                        applicationContext.packageName,
                        0
                    ).versionCode
                )
            }
            "isDeviceRooted" -> InstallUtils(applicationContext, mActivity).isDeviceRooted(call, result)
            "isSystemApplication" -> result.success(
                InstallUtils(applicationContext, mActivity).isSystemApplication(
                    applicationContext.packageManager,
                    applicationContext.packageName
                )
            )
            "checkShellRootPermission" -> result.success(
                InstallUtils(
                    applicationContext,
                    mActivity
                ).checkRootPermission()
            )
            "installApk" -> InstallUtils(applicationContext, mActivity).installApk(call, result)
            else -> {
                result.notImplemented()
            }
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        mActivity = binding.activity
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        mActivity = binding.activity
    }

    override fun onDetachedFromActivity() {
        mActivity = null
    }
}
